import { useEffect, useState } from 'react'
import { getUserinfoListApi, DelAdminUser } from '@/apis/adminuser'
import { Authentication } from '@/apis/user'
import { message } from 'antd'
import { useDispatch, useSelector } from 'react-redux'

//获取用户列表信息
function useAdminUserList() {
    //获取文章列表
    const [userInfoList, setUserInfoList] = useState([])
    //用户鉴权
    const [authentication, setAuthentication] = useState({})
    //设置当前 id
    const [currentId, setCurrentId] = useState("")
    const { mobile, is_superuser } = useSelector(state => state.user.userInfo)
    //获取文章列表参数
    const [reqData, setReqData] = useState({
        page: 1,
        is_superuser: null,
        username: null,
        mobile: null,
        MaxResultCount: 20,
        SkipCount: 0
    })

    const [count, setCount] = useState(0)

    //获取筛选数据
    const onFinish = (formValue) => {
        // console.log(formValue)
        // 1. 准备参数
        const { username, mobile } = formValue
        setReqData({
            ...reqData,
            // isadmin: administrator,
            page: 1,
            is_superuser: true,
            username: username,
            mobile: mobile,
            MaxResultCount: 20,
            SkipCount: 0
        })
    }

    //获取数据列表
    const getList = async function getList() {
        //用户鉴权
        // const auth_res = await Authentication()
        // setAuthentication(auth_res)
        const res = await getUserinfoListApi(reqData)
        setCount(res.data.totalCount)
        setUserInfoList(res.data.items)
        setCurrentId(res.data.id)
    }

    //文章列表
    useEffect(() => {
        getList()
    }, [reqData])

    //列表分页
    const onPageChange = (val) => {
        // console.log(val)
        setReqData({
            ...reqData,
            page: val,
            MaxResultCount: 20,
            SkipCount: (val - 1) * 20
        })
    }

    //删除用户
    const onConfirm = async (id) => {
        // if (authentication.code != 200) {
        //     message.error(authentication.message)
        //     return
        // }
        if (is_superuser == "0") {
            message.error("您没有权限")
            return
        }
        // console.log(id)
        // return
        const res = await DelAdminUser(id)
        if (res.code == 200) {
            message.success("删除成功")
            getList()
        }
        else {
            message.error(res.message)
        }
    }

    return { currentId, authentication, reqData, count, userInfoList, onPageChange, onConfirm, onFinish, getList }
}

export default useAdminUserList